require 'spider'
class ListTask < ActiveRecord::Base
  validates_uniqueness_of :cat, :message => '列表任务分类ID重复'
  include Spider

  def self.create_from_first(cat_url)
    doc = open_page(cat_url)
    if doc.nil?
      Rails.logger.warn "抓取一级分类页出错, url: #{cat_url}" 
    else
      cate = doc.css('div.cate-item > ul > li')
      Rails.logger.warn("抓取一级分类连接错误1, url: #{cat_url}") && return if cate.blank?
      list_task = ListTask.new
      list_task.url = cat_url
      select_cate_tag = cate.css('ul > li').first.children[1]
      if select_cate_tag.name == 'strong'
        cat_shop_display_url = doc.css('div.filter > ul > li > a[title="按店铺展示"]').first['href']
        list_task.cat = cat_shop_display_url.scan(/cat=(\d+)/).to_s.to_i
      elsif select_cate_tag.name == 'a'
        list_task.cat = select_cate_tag['href'].scan(/cat=(\d+)/).to_s.to_i
      end
      Rails.logger.warn("抓取一级分类连接错误2 url: #{cat_url}") && return if list_task.cat.blank?
      list_task.save!
    end
  end

  def create_for_cat(cat_url = nil)
    cat_url = category_url if cat_url.nil?
    doc = open_page(cat_url)
    if doc.nil?
      Rails.logger.warn "抓取分类页出错, url: #{cat_url}" 
    else
      alinks = doc.css('a')
      alinks = alinks.map{|a| a['href'] =~ /cat=\d+/ ? a['href'] : nil}.uniq
      alinks.delete nil
      cats = alinks.map{|alink| alink.scan(/cat=(\d+)/).to_s}.uniq
      cats.each do |c|
        list_task = ListTask.new
        list_task.url = cat_url #存的是来源网址,而不是采集来的原始地址
        list_task.cat = c.to_i
        list_task.save! rescue Rails.logger.info "分类ID重复,cat: #{c}"
      end
    end
  end

  def category_url(num = 1)
    "http://list.tmall.com/search_product.htm?style=w&n=20&s=#{(num-1)*20}&cat=#{cat}&prt=1314274565329&prc=3"
  end
end
